package tc.crase.cmds;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tc.common.utility.Dynbuff;
import tc.common.utility.DynbuffLine;
import tc.common.utility.DynbuffRect;
import tc.common.utility.GrView;
import tc.common.utility.TcCmd;

public class CmdViewZoom  extends TcCmd implements ActionListener {
	
	double factor = 0.8;
	
	JLabel statusLabel;
	
	public CmdViewZoom () {
	}
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Zoom");
		setHint("Enter point.");
		
		statusLabel = new JLabel("");
		
		JButton changeButton = new JButton("Change");
		changeButton.setActionCommand("change");
		changeButton.addActionListener(this);

		dialog.add(statusLabel);
		dialog.add(changeButton);
		
		update();
		
		finalizeDialog();
	}
	
	void update() {
		statusLabel.setText("Factor: " + factor);
	}
	
	public void enterPoint(Point pt, GrView view) {
		view.transf.scaleX *= factor;
		view.transf.scaleY *= factor;
		
		Dynbuff.dynamicBufferClean();
		
		view.repaint();
	}
	public void enterReset(GrView view) {
	}
	public void enterMove(Point pt, GrView view)  {
		Dynbuff.dynamicBufferClean();
		//if ( factor >= 0 ) return;
		
		double w = view.getWidth();
		double h = view.getHeight();
		
		double k = 1/factor; 
		
		Rectangle r = new Rectangle( (int)(w/2-k*w/2), (int)(h/2-k*h/2),
				                     (int)(w*k), (int)(h*k));
		Dynbuff.dynamicBufferAdd(new DynbuffRect(r), Dynbuff.Buffer.CMD, (JPanel)view);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (factor > 1.0) {
			factor = 0.8;
		}
		else {
			factor = 1.25;
		}
			
		update();
	}
}
